package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.BeanUtils;
import com.lantosec.hmrp.core.util.ExcelUtils;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.entity.SysUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.service.ISysUserService;
import com.lantosec.hmrp.modules.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * SysUser控制器
 * 用户表
 *
 * @author AutoCode
 * @since 2020-02-07
 */
@Api(value = "用户管理",tags = "用户管理")
@RestController
@RequestMapping("/user")
@Log4j2
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService service;

    @Autowired
    ISysDeptService deptService;


    /*@RequiresRoles("admin")
    @RequiresPermissions("query")*/
    @GetMapping("all")
    @ApiOperation("查询全部")
    public Result<List<SysUserVO>> all() {
        List<SysUser> sysUserList = service.getAll();
        return Result.ok(entity2VO(sysUserList));
    }

    @GetMapping("queryUserList")
    @ApiOperation("分页查询")
    public Result<Page<SysUserVO>> page(PageVO<SysUser> page,
        @ApiParam(value = "员工姓名") @RequestParam(name = "name",required = false) String name,
        @ApiParam(value = "工号") @RequestParam(name = "username",required = false) String username,
        @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
        @ApiParam(value = "状态") @RequestParam(name = "state",required = false) String state) {
        return Result.ok(service.queryUserList(page,name,username,deptId,state));
    }

    @GetMapping("get/{id}")
    @ApiOperation("主键查询")
    public Result<SysUserVO> get(@PathVariable(name = "id") String id) {
        SysUser user = service.getById(id);
        if(Objects.isNull(user)){
            return Result.fail("用户查询失败");
        }
        return Result.ok(entity2VO(user));
    }

    @PostMapping("insertUserInfo")
    @ApiOperation("新增数据人员信息")
    public Result<String> insertOrUpdateUserInfo(
            @RequestBody @Validated @ApiParam(value = "sysUserVO", required = true) SysUserVO vo) {
        String userId = service.insertOrUpdateUserInfo(vo);
        return Result.ok(userId);
    }

    @PutMapping("updateUserInfo")
    @ApiOperation("修改数据")
    public Result updateUserInfo(@RequestBody SysUserVO vo) {
        service.updateUserInfo(vo);
        return Result.ok();
    }

    @PutMapping("resetPassword")
    @ApiOperation("重置密码")
    public  Result<Void> resetPassword(
            @ApiParam(value = "用户ID",required = true) @RequestParam(name = "userId") String userId
    ){
        Integer state = service.resetPassword(userId);
        if (state == 1){
            return Result.ok();
        }else {
            return Result.fail("重置密码失败");
        }
    }

    @PutMapping("updatePassword")
    @ApiOperation("修改用户密码")
    public  Result<Void> updatePassword(@ApiParam(value = "用户信息",required = true) @RequestBody SysUserVO vo){
        Result result = service.updatePassword(vo);
        return result;
    }

    @DeleteMapping("delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@PathVariable(name = "ids") String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("export")
    @ApiOperation("导出数据")
    public void export(HttpServletResponse response) {
        ExcelUtils.exportExcel(SysUserVO.class, all().getData(), true, "系统用户表信息", null, response);
    }

    @PostMapping("import")
    @ApiOperation("导入数据")
    public void export(MultipartFile file) throws IOException {
        int count = 0;
        List<SysUserVO> list = ExcelUtils.importExcel(SysUserVO.class, file.getInputStream(), null);
        service.saveBatch(VO2Entity(list));
    }

    @GetMapping("queryPositionByDeptId")
    @ApiOperation("根据部门ID查询岗位信息")
    public Result<String> queryPositionByDeptId(
            @ApiParam(value = "部门ID",required = true) @RequestParam(name = "deptId") String deptId
    ){
        String positions = deptService.getById(deptId).getPositions();
        return Result.ok(positions);
    }

    @GetMapping("queryAllRoleIdAndName")
    @ApiOperation("查询角色ID跟角色名称")
    public Result queryAllRole(){
        List param = service.queryAllRole();
        return Result.ok(param);
    }

    @GetMapping("queryRoleByUserId")
    @ApiOperation("查询当前用户的角色信息")
    public Result<List> queryRoleByUserId(
            @ApiParam(value = "用户ID",required = true) @RequestParam(name = "userId") String userId
    ){
        List param = service.queryRoleByUserId(userId);
        return Result.ok(param);
    }

    @PutMapping("saveUserRoleInfo")
    @ApiOperation("保存用户角色信息")
    public Result saveUserRoleInfo(
                @ApiParam(value = "角色ID", required = true) @RequestParam(name = "roleIds") String roleIds,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId") String userId
    ) {
        service.saveUserRoleInfo(userId, roleIds);       //保存用户角色信息 sys_user_role
        return Result.ok();
    }

    @GetMapping("queryAllDeptNameAndId")
    @ApiOperation("查询所有部门信息")
    public Result<List<SysDept>> queryAllDeptNameAndId(){
        List<SysDept> list = deptService.list();
        return Result.ok(list);
    }

    @GetMapping("queryDeptUserList")
    @ApiOperation("部门人员查询")
    public Result<PageVO<SysUserVO>> queryDeptUserList(PageVO<SysUserVO> page, @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId
            ,@ApiParam(value = "岗位Id") @RequestParam(name = "positionId",required = false) String positionId
            ,@ApiParam(value = "姓名") @RequestParam(name = "name",required = false) String name) {
        service.queryDeptUserList(page, deptId, positionId, name);
        return Result.ok(page);
    }

    @PutMapping("updateDeptUserInfo")
    @ApiOperation("部门人员信息变更")
    public Result<Void> updateDeptUserInfo(@ApiParam(value = "用户信息",required = true) @RequestBody SysUserVO vo){
        SysUser entity = vo.convertToEntity();
        Boolean parm = service.updateById(entity);
        if(parm){
            return Result.ok();
        }else {
            return Result.fail("操作失败");
        }
    }

    @GetMapping("queryUniqueness")
    @ApiOperation("手机号码与用户名唯一性检测")
    public Result<Void> queryUsernameAndPhoneUniqueness(
            @ApiParam(value = "员工姓名") @RequestParam(name = "userName",required = false) String userName,
            @ApiParam(value = "手机号码") @RequestParam(name = "phone",required = false) String phone,
            @ApiParam(value = "0新增,1修改") @RequestParam(name = "flag",required = false) Integer flag
    ){
        if(flag == 0){
            Integer param = service.queryUsernameAndPhoneUniqueness(userName,phone);
            return param != 0 ? Result.fail("用户名或手机号码已存在！") : Result.ok();
        }else{
            Integer param = service.updateUsernameAndPhone(userName,phone);
            return param != 0 ? Result.fail("手机号码已存在！") : Result.ok();
        }
    }

    @GetMapping("queryUserByDeptId")
    @ApiOperation("根据部门ID查序部门下人员")
    public Result<List<SysUserVO>> queryUserByDeptId(
            @ApiParam(value = "deptId", required = true) @RequestParam(name="deptId") String deptId){
        List<SysUser> param = service.queryUserByDeptId(deptId);
        return Result.ok(BeanUtils.entity2VO(param));
    }

    @GetMapping("query")
    @ApiOperation("查询部门/岗位下所有人员")
    public Result<List<SysUserVO>> query(
            @ApiParam(value = "部门Id", required = true) @RequestParam(name="deptId") String deptId,
            @ApiParam(value = "岗位Id") @RequestParam(name="positionId") String positionId) {
        List<SysUser> param = service.queryUser(deptId, positionId);
        return Result.ok(entity2VO(param));
    }
}

